package com.effectiv.gooruda.rest.api.service;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.QueueBrowser;
import javax.jms.Session;

import lombok.Getter;
import lombok.Setter;

import org.springframework.jms.core.BrowserCallback;

public class SimpleBrowserCallback implements BrowserCallback<Long> {
	
	private int max;
	
	
	
	public SimpleBrowserCallback(int max) {
		this.max = max;
	}

	@Getter @Setter
	private List<Message> messages;
	
	@Override
	public Long doInJms(Session session, QueueBrowser browser) throws JMSException {
		Enumeration enumeration = browser.getEnumeration();
		
		
        long counter = 0;
        messages = new ArrayList<Message>();
        while (enumeration.hasMoreElements()) {
            Message msg = (Message) enumeration.nextElement();
            System.out.println(String.format("\tFound : %s", msg));
            messages.add(msg);
            counter += 1;
            
            if(counter >= max)
            	break;
        }
        return counter;

	}

}
